package com.snatch.snatchmgr.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.snatch.common.vo.NodeTaskInput;
import com.snatch.snatchmgr.core.SnatchCtrlManage;
import com.snatch.snatchmgr.service.SnatchService;
import com.snatch.snatchmgr.system.SystemData;
import com.snatch.snatchmgr.vo.SnatchGame;
import com.snatch.snatchmgr.vo.SnatchItem;

@Service
public class SnatchServiceImpl implements SnatchService {

	
	@Autowired
	private SnatchCtrlManage snatchCtrlManage;
	@Override
	public String startSnatch(String webCode, String gameCode,String itemName,String itemType) {
		SnatchGame snatchGame = SystemData.findSnatchGame(webCode,gameCode);
		if(null== snatchGame) return "-1";
		for(SnatchItem snatchItem : snatchGame.getSnatchItems()){
			if(null == itemName || null == itemType ||  (itemName.equals(snatchItem.getItemName()) && itemType.equals(snatchItem.getItemType()))){
				 NodeTaskInput nodeTaskInput = new NodeTaskInput();
				 nodeTaskInput.setWebCode(webCode);
				 nodeTaskInput.setGameCode(gameCode);
				 nodeTaskInput.setSleepTiem(snatchItem.getSleepTiem());
				 nodeTaskInput.setItemName(snatchItem.getItemName());
				 nodeTaskInput.setItemType(snatchItem.getItemType());
				 nodeTaskInput.setRatioType(snatchItem.getRatioType());
				 nodeTaskInput.setTaskCode(snatchItem.itemOnlyCode());
				snatchCtrlManage.initSnatchItem(nodeTaskInput);
			}
		}
		return "1";
	}
	@Override
	public SnatchGame findItem(String webCode,String gameCode) {
		return SystemData.findSnatchGame(webCode,gameCode);
	}
	@Override
	public List<SnatchGame> findGames(String webCode) {
		List<SnatchGame> temlist = new ArrayList<SnatchGame>();
		List<SnatchGame> snatchGames = SystemData.findSnatchGames(webCode);
		if(null != snatchGames){
			for(SnatchGame snatchGame : snatchGames){
				SnatchGame temSnatchGame = new SnatchGame();
				temSnatchGame.setGameName(snatchGame.getGameName());
				temSnatchGame.setGameCode(snatchGame.getGameCode());
				temlist.add(temSnatchGame);
			}
		}
		return temlist;
	}

}
